package leetcode;

import java.util.ArrayList;
import java.util.List;


public class ObserverStrategy {
	public static void main(String[] args){
		//------------------初始化杂志和观察者--------------------
		Magzine duzheMagzine = new MagA();
		Magzine gaoxiaoMagzine = new MagB();

		duzheMagzine.name = "duzhe";
		gaoxiaoMagzine.name = "gaoxiao";
		
		Observer aObserver = new Ghost(duzheMagzine);
		Observer bObserver = new Guang(gaoxiaoMagzine);
		//------------------观察者进行订阅------------------------
		duzheMagzine.add(aObserver);
		gaoxiaoMagzine.add(bObserver);
		
		//------------------杂志有更新---------------------------
		duzheMagzine.status = "version1";
		gaoxiaoMagzine.status= "version2";
		
		//------------------主动通知读者--------------------------
		duzheMagzine.notifyy();
		gaoxiaoMagzine.notifyy();
	}
}

interface Observer{
	public void update();
}

class Ghost implements Observer{
	private Magzine magzine = null;
	public Ghost(Magzine magzine){
		this.magzine = magzine;
	}
	@Override
	public void update() {
		System.out.println("Ghost:the magzine"+magzine.name +"has been updated");
	}
}
class Guang implements Observer{
	private Magzine magzine = null;
	public Guang(Magzine magzine){
		this.magzine = magzine;
	}
	@Override
	public void update(){
		System.out.println("Guang:the magzine"+magzine.name+"has been updated");
	}
}

abstract class Magzine{
	protected String name;
	protected String status;
	protected List<Observer> observers = new ArrayList<Observer>();
	abstract public void add(Observer observer);
	abstract public void rm(Observer observer);
	abstract public void notifyy();
}

class MagA extends Magzine{

	@Override
	public void add(Observer observer) {
		observers.add(observer);
	}

	@Override
	public void rm(Observer observer) {
		observers.remove(observer);
	}

	@Override
	public void notifyy() {
		for(Observer ob:observers){
			ob.update();
		}
	}
}


class MagB extends Magzine{

	@Override
	public void add(Observer observer) {
		observers.add(observer);
	}

	@Override
	public void rm(Observer observer) {
		observers.remove(observer);
	}

	@Override
	public void notifyy() {
		for(Observer ob:observers){
			ob.update();
		}
	}
	
}